package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCostStandardConf;
import cn.stylefeng.guns.modular.basic.model.params.BasicCostStandardConfParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCostStandardConfResult;
import cn.stylefeng.guns.modular.basic.service.BasicCostStandardConfService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.result.DictResult;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;


/**
 * 成本标准配置表控制器
 *
 * @author zhangzh
 * @Date 2024-05-08 17:51:48
 */
@Controller
@RequestMapping("/basicCostStandardConf")
public class BasicCostStandardConfController extends BaseController {

    private String PREFIX = "/basic/basicCostStandardConf";

    @Autowired
    private BasicCostStandardConfService basicCostStandardConfService;

    @Autowired
    private UserService userService;

    @Autowired
    private DictService dictService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("")
    public String index(Model model) {
        List<DictResult> dictList = dictService.findByDicttypeCode2("VEH_LIMIT_TYP");//车限类型  重量 ；体积 字典
        model.addAttribute("vehLimitTypList", dictList);

        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/basicCostStandardConf.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("/add")
    public String add(Model model) {
        List<DictResult> dictList = dictService.findByDicttypeCode2("VEH_LIMIT_TYP");//车限类型  重量 ；体积 字典
        model.addAttribute("vehLimitTypList", dictList);

        List<DictResult> costRatList = dictService.findByDicttypeCode2("COST_RAT");//成本标准折扣 字典
        model.addAttribute("costRatList", costRatList);
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));

        return PREFIX + "/basicCostStandardConf_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        List<DictResult> dictList = dictService.findByDicttypeCode2("VEH_LIMIT_TYP");//车限类型  重量 ；体积 字典
        model.addAttribute("vehLimitTypList", dictList);

        List<DictResult> costRatList = dictService.findByDicttypeCode2("COST_RAT");//成本标准折扣 字典
        model.addAttribute("costRatList", costRatList);

        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/basicCostStandardConf_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicCostStandardConfParam basicCostStandardConfParam) {
        //set 交付id
        Long DeliverId = this.userService.getDeliverId();
        if (DeliverId != null) {
            basicCostStandardConfParam.setDeliverId(DeliverId);
        }
        //set 部门ID
        Long deptId = this.userService.getDeptId();
        if (deptId != null) {
            basicCostStandardConfParam.setDeptId(deptId);
        }
        //set 用户ID
        Long userId = this.userService.getUserId();
        if (userId != null) {
            basicCostStandardConfParam.setCreateUser(userId);
            basicCostStandardConfParam.setUpdateUser(userId);
        }
        //创建时间
        basicCostStandardConfParam.setCreateTime(new Date());

        this.basicCostStandardConfService.add(basicCostStandardConfParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicCostStandardConfParam basicCostStandardConfParam) {
        //set 用户ID
        Long userId = this.userService.getUserId();
        if (userId != null) {
            basicCostStandardConfParam.setUpdateUser(userId);
        }
        this.basicCostStandardConfService.update(basicCostStandardConfParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicCostStandardConfParam basicCostStandardConfParam) {
        this.basicCostStandardConfService.delete(basicCostStandardConfParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicCostStandardConfParam basicCostStandardConfParam) {
        BasicCostStandardConf detail = this.basicCostStandardConfService.getById(basicCostStandardConfParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicCostStandardConfParam basicCostStandardConfParam, String condition) {
        basicCostStandardConfParam.setDeliverId(userService.getDeliverId());
        if (basicCostStandardConfParam.getProId() == null) {
            basicCostStandardConfParam.setProId(-1L);
        }
        return this.basicCostStandardConfService.findPageBySpec(basicCostStandardConfParam, condition);
    }


    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-05-08
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<BasicCostStandardConfResult> listAll(BasicCostStandardConfParam basicCostStandardConfParam, String condition) {
        basicCostStandardConfParam.setDeliverId(userService.getDeliverId());
        if (basicCostStandardConfParam.getProId() == null) {
            basicCostStandardConfParam.setProId(-1L);
        }
        return this.basicCostStandardConfService.findAll(basicCostStandardConfParam, condition);
    }

    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {

        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));

        return PREFIX + "/import.html";
    }

    /**
     * 下载模板
     *
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcel")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "assets/excel/basicCostStandardConf.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("成本标准配置" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入excel
     */
    @RequestMapping(value = "/importExcel", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file,BasicCostStandardConfParam basicCostStandardConfParam) {
        ResponseData data = new ResponseData();
        User user = userService.getcurrentUser();
        basicCostStandardConfParam.setDeliverId(user.getDeliverId());
        basicCostStandardConfParam.setDeptId(user.getDeptId());
        basicCostStandardConfParam.setCreateTime(new Date());
        basicCostStandardConfParam.setCreateUser(user.getUserId());
        basicCostStandardConfParam.setUpdateUser(user.getUserId());
        try {
            data = basicCostStandardConfService.getExceldata(file, basicCostStandardConfParam, data);//处理导入数据
        } catch (Exception e) {
            data.setSuccess(false);
            e.printStackTrace();
        }
        return data;
    }

}


